package com.dlc.felear.xiaoensale.activity;

import android.text.Editable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ListView;

import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.adapter.EmnuStdListtemAdapter;
import com.dlc.felear.xiaoensale.adapter.LeftMenuCatAdapter;
import com.dlc.felear.xiaoensale.constant.Constant;
import com.dlc.felear.xiaoensale.customView.ConfirmDialog;
import com.dlc.felear.xiaoensale.dao.CookingMenuDao;
import com.dlc.felear.xiaoensale.dao.OperatorDao;
import com.dlc.felear.xiaoensale.deviceUtil.TopbarHelper;
import com.dlc.felear.xiaoensale.entity.CookingMenu;
import com.dlc.felear.xiaoensale.helper.CookingMenuMangerHelper;
import com.dlc.felear.xiaoensale.helper.JsonHelper;
import com.dlc.felear.xiaoensale.helper.Session;
import com.itdlc.android.library.base.BaseActivity;
import com.itdlc.android.library.listener.TextWatcherAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * 标准菜谱库页面
 */
public class MenuListActivity extends BaseActivity  {
    private static final String TAG = "MenuListActivity";
    @BindView(R.id.e_gv)
    GridView gv;
    @BindView(R.id.c_lv)
    ListView lv;
    @BindView(R.id.et_search)
    EditText etSeach;
    private CookingMenuDao cookingMenuDao;
    private EmnuStdListtemAdapter adapter;
    private TopbarHelper topbarHelper;
    private LeftMenuCatAdapter dialogCatAdapter;
    private CookingMenuMangerHelper cookingMenuMangerHelper;
    private String deviceType="M400";
    private String catName="全部";


    @Override
    protected int getLayoutRes() {
        return R.layout.activity_menu_list;
    }

    @Override
    protected void initialView() {
        cookingMenuDao = new CookingMenuDao(this);
        cookingMenuMangerHelper=CookingMenuMangerHelper.getInstance(this,cookingMenuDao);
        dialogCatAdapter = new LeftMenuCatAdapter(this);
        topbarHelper = new TopbarHelper(this);
        topbarHelper.setTitle("标准菜谱列表");
        topbarHelper.setNavActivity(CcLauncherActivity.class);
        adapter = new EmnuStdListtemAdapter(this);
        gv.setAdapter(adapter);
        deviceType=Session.getSession().getString(Constant.D_GT_TYPE,"M400");
        etSeach.addTextChangedListener(new TextWatcherAdapter() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                super.beforeTextChanged(s, start, count, after);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                adapter.setItems(checkData(cookingMenuDao.queryForNameByType(s.toString(), Constant.MENU_STD)));
            }

            @Override
            public void afterTextChanged(Editable s) {
                super.afterTextChanged(s);
            }
        });
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                dialogCatAdapter.setSelectitem(position);
                handleCat(dialogCatAdapter.getItem(position));
            }
        });
        lv.setAdapter(dialogCatAdapter);
        dialogCatAdapter.setItems(cookingMenuMangerHelper.getStdCatListNames());
    }


    @Override
    protected void initData() {
        initDaoData();
    }

    private void initDaoData() {
        adapter.setItems(checkData(cookingMenuMangerHelper.getCookingData(deviceType,Constant.MENU_STD)));
    }





    private void handleCat(String catName){
        this.catName=catName;
        adapter.setItems(checkData(cookingMenuMangerHelper.getCatMenuList(catName,deviceType,Constant.MENU_STD)));
    }

    private List<CookingMenu> checkData(List<CookingMenu> data) {
        if (data == null || data.isEmpty()) {
            return new ArrayList<>();
        }
        Map<String, CookingMenu> tempMap = new HashMap<>();
        try {
            for (CookingMenu cookingMenu : data) {
                boolean namerr = cookingMenu.getName().contains(cookingMenuMangerHelper.changeValue(catName));
                if (TextUtils.equals(catName, getString(R.string.quanbu))||TextUtils.equals(catName,  getString(R.string.jinricaipin))) {
                    namerr = true;
                }
                if (Integer.parseInt(cookingMenu.getSpecification()) <= 1 && namerr) {
                    tempMap.put(cookingMenu.getName() + cookingMenu.getSpecification(), cookingMenu);
                }
            }
        } catch (Exception e) {
            Log.e(TAG, "checkData Exception:" + e.getMessage());
        }
        return new ArrayList<>(tempMap.values());
    }

}